package com.android.thelastnoet.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.android.thelastnoet.bean.Note;
import com.android.thelastnoet.bean.User;

public class UserMapper {
    private static UserMapper userMapper = null;
    private DBOpenHelper dbOpenHelper;
    private SQLiteDatabase db;

    public UserMapper(Context context) {
        dbOpenHelper = DBOpenHelper.getDbOpenHelper(context);
    }

    public static synchronized UserMapper getUserMapper(Context context) {
        if (userMapper == null) {
            userMapper = new UserMapper(context);
        }
        return userMapper;
    }

    //获取用户信息
    public User getUserInfo(String userName) {
        User user=null;
        db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(DBOpenHelper.TABLE.USER, null, "userName = ? ", new String[]{userName}, null, null, null);
        if (cursor != null) {
            user=new User();
            /*！！！游标越界*/
            cursor.moveToFirst();
            int id = cursor.getInt(cursor.getColumnIndex("id"));
//            String userName = cursor.getString(cursor.getColumnIndex("userName"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
//            String secretLock = cursor.getString(cursor.getColumnIndex("secretLock"));

            user.setId(id);
            user.setUserName(userName);
            user.setPassword(password);
//            user.setSecretLock(secretLock);

        } else {
            Log.d("mapper", "找不到");

        }
        cursor.close();
        return user;
    }

    /*添加用户*/
    public long addUser(User user) {
        db = dbOpenHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("userName", user.getUserName());
        contentValues.put("password", user.getPassword());

        long id = db.insert(DBOpenHelper.TABLE.USER, null, contentValues);
        return id;
    }

    public void deleteUser(int userId){

    }
}
